package com.icore.ui;

import org.apache.http.message.BasicNameValuePair;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.icore.AppContext;
import com.icore.AppException;
import com.icore.R;
import com.icore.bean.Gouche;
import com.icore.bean.GoucheList;
import com.icore.bean.RetRes;
import com.icore.common.UIHelper;
import com.icore.ui.adapter.GoucheAdapter;
import com.icore.ui.base.BaseActivity;

/**
 * 购车列表
 * 
 */
public class GoucheListActivity extends BaseActivity {

	private String userID = "";
	private String carNo = "";

	private ImageView editIV;
	private PopupWindow popupWindow;
	private LinearLayout layout;
	private ListView listView;
	private String title[] = { "新建购车单" };

	private ListView goucheLV;

	/** 请求类型 */
	public int REQ_Type_INT = 0;
	public static final int LIST_QUERY = 1; // 查询列表
	public static final int STATE_UPDATE = 2; // 更改状态

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gouche_list_view);

		// 获取传递过来的数据
		getIntentEntra();

		// 返回
		ImageView backIV = (ImageView) findViewById(R.id.backID);
		backIV.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}

		});

		editIV = (ImageView) findViewById(R.id.editID);
		editIV.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				int x = getWindowManager().getDefaultDisplay().getWidth() * 2 / 3;
				int y = editIV.getBottom() * 3 / 2;

				showPopupWindow(x, y);
			}

		});

		// 列表
		goucheLV = (ListView) findViewById(R.id.goucheLV);
		goucheLV.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Gouche gouche = (Gouche) parent.getAdapter().getItem(position);
				carNo = gouche.getCarNo();

				// 更新状态
				reqData(STATE_UPDATE);
			}
		});

		reqData(LIST_QUERY);
	}

	private void getIntentEntra() {
		Intent intent = getIntent();
		userID = intent.getStringExtra("user_id");
	}

	private ProgressDialog mProgress;

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (mProgress != null) {
				mProgress.dismiss();
			}
			if (msg.what == 1) {
				int code = 0;
				String resMsg = "";

				switch (REQ_Type_INT) {
				case LIST_QUERY:
					GoucheList goucheList = (GoucheList) msg.obj;
					code = goucheList.getResCode();
					resMsg = goucheList.getResMessage();

					// 失败，弹出提示框
					if (code == 0) {
						UIHelper.ToastMessage(GoucheListActivity.this, goucheList.getResMessage());
					}
					// 成功
					else if (code == 1) {
						GoucheAdapter goucheAdapter = new GoucheAdapter(GoucheListActivity.this);
						goucheAdapter.setList(goucheList.getList());
						goucheLV.setAdapter(goucheAdapter);
					}
					break;
				case STATE_UPDATE:
					RetRes retRes = (RetRes) msg.obj;
					code = retRes.getResCode();
					resMsg = retRes.getResMessage();

					// 失败，弹出提示框
					if (code == 0) {
						UIHelper.ToastMessage(GoucheListActivity.this, resMsg);
					}
					// 成功
					else if (code == 1) {
						UIHelper.startActivity(GoucheListActivity.this, GoucheDetailActivity.class, new BasicNameValuePair("car_no", carNo));
					}
					break;
				}
			} else {
				((AppException) msg.obj).makeToast(GoucheListActivity.this);
			}
		}
	};

	private void reqData(int reqType) {
		REQ_Type_INT = reqType;
		mProgress = ProgressDialog.show(GoucheListActivity.this, null, "加载中...", true, true);

		new Thread() {

			@Override
			public void run() {
				Message msg = new Message();
				try {
					switch (REQ_Type_INT) {
					case LIST_QUERY:
						GoucheList goucheList = AppContext.server.getGoucheList(GoucheListActivity.this.userID);
						msg.what = 1;
						msg.obj = goucheList;
						break;
					case STATE_UPDATE:
						RetRes retRes = AppContext.server.updateState(carNo);
						msg.what = 1;
						msg.obj = retRes;
						break;
					}
				} catch (AppException e) {
					e.printStackTrace();
					msg.what = -1;
					msg.obj = e;
				}
				handler.sendMessage(msg);
			}
		}.start();
	}

	public void showPopupWindow(int x, int y) {
		layout = (LinearLayout) LayoutInflater.from(GoucheListActivity.this).inflate(R.layout.pop_group, null);
		listView = (ListView) layout.findViewById(R.id.lv_dialog);
		listView.setAdapter(new ArrayAdapter<String>(GoucheListActivity.this, R.layout.pop_item, R.id.tv_text, title));

		popupWindow = new PopupWindow(GoucheListActivity.this);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.setWidth(getWindowManager().getDefaultDisplay().getWidth() / 3);
		popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setFocusable(true);
		popupWindow.setContentView(layout);
		popupWindow.showAtLocation(findViewById(R.id.titleBarRL), Gravity.LEFT | Gravity.TOP, x, y + 5);// 需要指定Gravity，默认情况是center.

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				popupWindow.dismiss();
				popupWindow = null;

				// 跳转
				UIHelper.startActivity(GoucheListActivity.this, GoucheNewActivity.class, new BasicNameValuePair("user_id", userID));
			}
		});
	}
}